package com.cb.myzhxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cb.myzhxy.pojo.Admin;
import com.cb.myzhxy.pojo.LoginForm;
import com.cb.myzhxy.pojo.Student;
import com.cb.myzhxy.pojo.Teacher;
import com.cb.myzhxy.service.AdminService;
import com.cb.myzhxy.service.StudentService;
import com.cb.myzhxy.service.TeacherService;
import com.cb.myzhxy.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Api(tags = "系统控制器")
@RestController
@RequestMapping("/sms/system")
public class SystemController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 获取验证码图片
     */
    @ApiOperation("获取验证码图片")
    @GetMapping("/getVerifiCodeImage")
    public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response){
        //获取图片
        BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();
        //获取验证码字符串
        String verifiCode=String.valueOf(CreateVerifiCodeImage.getVerifiCode());
        //将验证码放入当前请求域
        HttpSession session = request.getSession();
        session.setAttribute("verifiCode",verifiCode);
        //将验证码图片通过输出流做出响应
        try {
            ImageIO.write(verifiCodeImage,"JPEG",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录请求验证
     */
    @ApiOperation("登录请求验证")
    @PostMapping("/login")
    public Result login(@RequestBody LoginForm loginForm,HttpServletRequest request){
        //获取用户提交的验证码和session域中的验证码
        HttpSession session = request.getSession();
        String systemVerifiCode=(String) session.getAttribute("verifiCode");
        String loginVerificode = loginForm.getVerifiCode();
        if("".equals(systemVerifiCode)){
            //session过期,验证码超时
            return Result.fail().message("验证码失效,请刷新后重试!");
        }
        if(!loginVerificode.equalsIgnoreCase(systemVerifiCode)){
            //验证码有误
            return Result.fail().message("验证码有误,请刷新后重新输入!");
        }
        //验证码使用完毕,移除当前请求域中的验证码
        session.removeAttribute("verifiCode");

        // 准备一个Map集合,用户存放响应的信息
        Map<String, Object> map = new HashMap<>();
        // 根据用户身份,验证登录的用户信息
        switch (loginForm.getUserType()){
            case 1://管理员身份
                try {
                    //调用服务层登录方法,根据用户提交的LoginInfo信息,查询对应的Admin对象,找不到返回Null
                    Admin admin=adminService.login(loginForm);
                    if(null!=admin){
                        // 登录成功,将用户id和用户类型转换为token口令,作为信息响应给前端
                        String token = JwtHelper.createToken(admin.getId().longValue(), 1);
                        map.put("token",token);
                    }else {
                        throw  new RuntimeException("用户名或者密码有误!");
                    }
                    return Result.ok(map);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    // 捕获异常,向用户响应错误信息
                    return Result.fail().message(e.getMessage());
                }
            case 2: // 学生身份
                try {
                    Student student=studentService.login(loginForm);
                    if(null!=student){
                        String token = JwtHelper.createToken(student.getId().longValue(), 2);
                        map.put("token",token);
                    }else {
                        throw  new RuntimeException("用户名或者密码有误!");
                    }
                    return Result.ok(map);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    return Result.fail().message(e.getMessage());
                }
            case 3: // 教师身份
                try {
                    Teacher teacher=teacherService.login(loginForm);
                    if(null!=teacher){
                        String token = JwtHelper.createToken(teacher.getId().longValue(), 3);
                        map.put("token",token);
                    }else {
                        throw  new RuntimeException("用户名或者密码有误!");
                    }
                    return Result.ok(map);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    return Result.fail().message(e.getMessage());
                }
        }
        // 查无此用户,响应失败
        return Result.fail().message("查无此用户");
    }

    /**
     * 通过token获取用户信息
     */
    @ApiOperation("通过token获取用户信息")
    @GetMapping("/getInfo")
    public Result getUserInfoByToken(@RequestHeader("token")String token){
        // 获取用户中请求的token 检查token 是否过期 20H
        boolean isEx = JwtHelper.isExpiration(token);
        if(isEx){
            return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
        }
        // 解析token,获取用户id和用户类型
        Long userId = JwtHelper.getUserId(token);
        Integer userType = JwtHelper.getUserType(token);

        // 准备一个Map集合用于存响应的数据
        Map<String,Object> map=new HashMap<>();
        switch (userType){
            case 1:
                Admin admin=adminService.getAdminById(userId);
                map.put("user",admin);
                map.put("userType",1);
                break;
            case 2:
                Student student=studentService.getStudentById(userId);
                map.put("user",student);
                map.put("userType",2);
                break;
            case 3:
                Teacher teacher=teacherService.getTeacherById(userId);
                map.put("user",teacher);
                map.put("userType",3);
                break;
        }
        return Result.ok(map);
    }

    /**
     * 头像上传统一入口
     */
    @ApiOperation("头像上传统一入口")
    @PostMapping("/headerImgUpload")
    public Result headerImgUpload(
            @ApiParam("文件二进制数据") @RequestPart("multipartFile")MultipartFile multipartFile){
        //使用UUID随机生成文件名
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        //生成新的文件名字
        String originalFilename = multipartFile.getOriginalFilename();
        String newFileName=uuid.concat(originalFilename.substring(originalFilename.lastIndexOf(".")));
        //生成文件的保存路径
        String portraitPath="D:/学习资料/学习笔记/框架/项目/尚硅谷-智慧校园/project/myzhxy/target/classes/public/upload/".concat(newFileName);
        //保存文件
        try {
            multipartFile.transferTo(new File(portraitPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //响应图片路径
        String path="upload/".concat(newFileName);
        return Result.ok(path);
    }

    /**
     * 修改密码
     */
    @ApiOperation("修改密码")
    @PostMapping("/updatePwd/{oldPwd}/{newPwd}")
    public Result updatePwd(@ApiParam("token口令") @RequestHeader("token")String token,
                            @ApiParam("原始密码") @PathVariable("oldPwd") String oldPwd,
                            @ApiParam("新密码 ") @PathVariable("newPwd") String newPwd){
        boolean expiration = JwtHelper.isExpiration(token);
        if(expiration){
            //token过期
            return Result.fail().message("token失效!");
        }
        //通过token获取当前登录的用户id
        Long userId = JwtHelper.getUserId(token);
        //通过token获取当前登录的用户类型
        Integer userType = JwtHelper.getUserType(token);
        // 将明文密码转换为暗文
        oldPwd= MD5.encrypt(oldPwd);
        newPwd= MD5.encrypt(newPwd);
        if(userType==1){
            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Admin::getId,userId.intValue()).eq(Admin::getPassword,oldPwd);
            Admin admin = adminService.getOne(queryWrapper);
            if(null!=admin){
                admin.setPassword(newPwd);
                adminService.saveOrUpdate(admin);
            }else {
                return Result.fail().message("原密码输入有误！");
            }
        }else if(userType==2){
            LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Student::getId,userId.intValue()).eq(Student::getPassword,oldPwd);
            Student student = studentService.getOne(queryWrapper);
            if(null!=student){
                student.setPassword(newPwd);
                studentService.saveOrUpdate(student);
            }else {
                return Result.fail().message("原密码输入有误！");
            }
        }else if(userType==3){
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teacher::getId,userId.intValue()).eq(Teacher::getPassword,oldPwd);
            Teacher teacher = teacherService.getOne(queryWrapper);
            if(null!=teacher){
                teacher.setPassword(newPwd);
                teacherService.saveOrUpdate(teacher);
            }else {
                return Result.fail().message("原密码输入有误！");
            }
        }
        return Result.ok();
    }
}
